Ext.define('SMS.controller.Menu',{
	extend:'Ext.app.Controller',
	refs:[
		{ref:'smsmenu',selector:'smstablepanel'},
		{ref:'tabPanel',selector:'smstablepanel'}
	],
	init:function(){
		this.control({
			'smsmenu':{
				itemmousedown:this.loadMenu
			}
		});
	},
	loadMenu:function(selModel,record){
		var menu_id = record.raw.id;
		var menu_title = record.raw.title;
		if(record.get('leaf')){
			var panel = Ext.getCmp(menu_id);
			if(!panel){
				panel = Ext.create(record.raw.comp,{
					id:menu_id,
					title:menu_title
				});
				/*panel={
					title:'New Tab '+record.get('id'),
					iconCls:'tabs',
					html:'Tab Body '+record.get('id')+'<br/><br/>',
					closable:true
				}*/
				this.openTab(panel,record.get('id'));
			}else{
				var main = Ext.getCmp('content-panel');
				main.setActiveTab(panel);
			}
		}
	},
	openTab:function(panel,id){
		var o = (typeof panel == 'string'? panel:id || panel.id);
		var main = Ext.getCmp('content-panel');
		var tab = main.getComponent(o);
		if(tab){
			main.setActiveTab(tab);
		}else if(typeof panel!='string'){
			panel.id = o;
			var p = main.add(panel);
			main.setActiveTab(p);
		}
	}
});